package com.ewanghuiju.app.presenter.pintuan;

import com.ewanghuiju.app.app.Constants;
import com.ewanghuiju.app.base.RxPresenter;
import com.ewanghuiju.app.base.contract.pintuan.PintuanOrderListContract;
import com.ewanghuiju.app.component.RxBus;
import com.ewanghuiju.app.model.DataManager;
import com.ewanghuiju.app.model.bean.response.PintuanOrderListResponBean;
import com.ewanghuiju.app.model.bean.response.ShareResponBean;
import com.ewanghuiju.app.model.event.SendEvent;
import com.ewanghuiju.app.model.http.response.BaseResponse;
import com.ewanghuiju.app.model.http.response.Optional;
import com.ewanghuiju.app.util.RxUtil;
import com.ewanghuiju.app.widget.CommonSubscriber;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Predicate;

/**
 * @author sj  qq:751943509
 * @class 拼团订单
 * @time 2020/2/16
 */
public class PintuanOrderListPresenter extends RxPresenter<PintuanOrderListContract.View> implements PintuanOrderListContract.Presenter {

    private DataManager mDataManager;

    @Inject
    public PintuanOrderListPresenter(DataManager mDataManager) {
        this.mDataManager = mDataManager;
    }

    @Override
    public void attachView(PintuanOrderListContract.View view) {
        super.attachView(view);
        registerEvent();
    }

    /**
     * 刷新商品列表
     */
    private void registerEvent() {
        addSubscribe(RxBus.getDefault().toFlowable(SendEvent.class)
                .compose(RxUtil.<SendEvent>rxSchedulerHelper())
                .filter(new Predicate<SendEvent>() {
                    @Override
                    public boolean test(@NonNull SendEvent sendEvent) throws Exception {
                        return sendEvent.getType() == Constants.TYPE_GET_SHOP_INFO;
                    }
                })
                .subscribeWith(new CommonSubscriber<SendEvent>(mView, "") {
                    @Override
                    public void onNext(SendEvent s) {
                        if (mView != null) {
                            mView.refreshContent();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        registerEvent();
                    }
                })
        );
    }

    @Override
    public void getData(int page, int order_type) {
        addSubscribe(mDataManager.getPtiorderList(page, order_type,"1")
                        .compose(RxUtil.rxSchedulerHelper())
                        .compose(RxUtil.handleMyResultCannull())
                        .subscribeWith(new CommonSubscriber<Optional<List<PintuanOrderListResponBean>>>(mView) {
                            @Override
                            public void onNext(Optional<List<PintuanOrderListResponBean>> optional) {
                                if (mView != null)
                                    mView.showContent(optional.getIncludeNull());
                            }

                            @Override
                            public void onError(Throwable e) {
                                super.onError(e);
                                if (mView != null)
                                    mView.showContentError();
                            }
                        })
        );
    }

    @Override
    public void getMoreData(int page, int order_type) {

    }

    @Override
    public void ptitakeDelivery(String order_id) {
        addSubscribe(mDataManager.ptitakeDelivery(order_id)
                .compose(RxUtil.rxSchedulerHelper())
                .compose(RxUtil.handleMyResultCannull())
                .subscribeWith(new CommonSubscriber<Optional<BaseResponse>>(mView) {
                    @Override
                    public void onNext(Optional<BaseResponse> optional) {
                        //收货成功
                        if (mView != null)
                            mView.ptitakeDelivery();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                })
        );
    }

    /**
     * 获取分享信息
     *
     * @param goodsid
     * @param groupid
     */
    @Override
    public void getPtiptGoodsShare(String goodsid, String groupid) {
        addSubscribe(mDataManager.ptiptGoodsShare(goodsid, groupid)
                .compose(RxUtil.rxSchedulerHelper())
                .compose(RxUtil.handleMyResultCannull())
                .subscribeWith(new CommonSubscriber<Optional<ShareResponBean>>(mView) {
                    @Override
                    public void onNext(Optional<ShareResponBean> optional) {
                        if (mView != null)
                            mView.showPtiptGoodsShare(optional.getIncludeNull());
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                })
        );
    }
}
